package com.tianheng.modbus.service;


import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import lombok.extern.slf4j.Slf4j;


/**************************************************
 * copyright (c) 2020, peng.cao@tianheng-uestc.com All Rights Reserved.
 * created by peng.cao
 * date:       2020/7/10 0010
 * since:      1.0.0.1
 * description:
 *
 **************************************************/
@Slf4j
public class OutBoundHandler extends ChannelOutboundHandlerAdapter {


    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if(msg instanceof byte[]){
            byte[] msg1 = (byte[]) msg;
            ByteBuf buffer = ctx.alloc().buffer(msg1.length);

            log.info("向设备下发信息是"+ BytesAndHex.bytesToHexString(msg1));
            buffer.writeBytes(msg1);
            ctx.writeAndFlush(buffer).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    System.out.println("发送成功信息给设备！");
                }
            });

        }
    }
}
